<?php

/**
 * This is the model class for table "tbl_cart".
 *
 * The followings are the available columns in table 'tbl_cart':
 * @property integer $id
 * @property integer $customer_id
 * @property integer $product_id
 * @property integer $amount
 * @property string $owner
 *
 * The followings are the available model relations:
 * @property Customer $customer
 * @property Product $product
 */
class Cart extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Cart the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_cart';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('customer_id, product_id', 'required'),
            array('customer_id, product_id, amount', 'numerical', 'integerOnly' => true),
            array('owner', 'length', 'max' => 45),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, customer_id, product_id, amount, owner', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'users' => array(self::BELONGS_TO, 'users', 'customer_id'),
            'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Giỏ hàng',
            'customer_id' => 'Khách hàng',
            'product_id' => 'Sản phẩm',
            'amount' => 'Số lượng',
        //'owner' => 'Owner',
        );
    }

    /**
     * Retrieves owner form user.
     * @return array of owner.
     */
    public static function getCartsOfOwner($owner = 'notset') {
        if ($owner == 'notset')
            $owner = Yii::app()->User->getState('owner');

        return Cart::model()->findAll('owner = :owner', array(':owner' => $owner));
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        if ($this->id == 0)
            $criteria->compare('id', $this->id);

        $criteria->compare('customer_id', $this->customer_id);
        $criteria->compare('product_id', $this->product_id);
        $criteria->compare('amount', $this->amount);
        $criteria->compare('owner', $this->owner, true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}